package com.yanqu.road.server.cmd.banquet.cross.goodbanquet;

import com.yanqu.road.entity.banquet.baifubanquet.GoodBanquetFinishReceiveData;
import com.yanqu.road.entity.log.LogGoodBanquetRecord;
import com.yanqu.road.entity.player.UserInfo;
import com.yanqu.road.pb.banquet.CrossBanquetProto;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.Command;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.banquet.CrossBanquetMgr;
import com.yanqu.road.server.manger.log.LogMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.date.DateHelper;
import io.netty.channel.ChannelHandlerContext;

import java.util.*;

/**
 * @Description 开启百福宴会 跨服返回
 * @Author cwq
 * @Data 2021/11/9 14:34
 */
@Cmd(code = Protocol.S_GOOD_BANQUET_SETTLEMENT_FROM_CROSS, desc = "百福宴结算 跨服返回区服 协议")
public class GoodBanquetSettlementFromCrossCmd implements Command {

    @Override
    public void execute(ChannelHandlerContext ctx, YanQuMessage packet) throws Exception {
        CrossBanquetProto.GoodBanquetSettlementListToGameServerMsg builder = CrossBanquetProto.GoodBanquetSettlementListToGameServerMsg.parseFrom(packet.getBytes());
        HashSet<Long> set = new HashSet<>();
        Map<Integer,List<Long>> activityBanquetIdMap = new HashMap<>();
        synchronized (GoodBanquetSettlementFromCrossCmd.class){
            for (CrossBanquetProto.GoodBanquetSettlementMsg item : builder.getSettlementListList()){
                UserInfo userInfo = UserMgr.getUserInfo(item.getUserId());
                if(userInfo == null){
                    CrossBanquetMgr.getLogger().error("百福宴下发的结算信息并不属于这个区服。id:{}，userId:{}",item.getBanquetId(),item.getUserId());
                    continue;
                }
                List<GoodBanquetFinishReceiveData> userFinishList = CrossBanquetMgr.getUserFinishGoodBanquetList(item.getUserId(),true);
                boolean duplicate = false;
                for (GoodBanquetFinishReceiveData finishReceiveData : userFinishList){
                    if(finishReceiveData.getBanquetId() == item.getBanquetId() && finishReceiveData.getActivityId() == item.getActivityId()){
                        CrossBanquetMgr.getLogger().error("重复下发结算百福宴。id:{} ,activityId;{}",item.getBanquetId(),item.getActivityId());
                        duplicate = true;
                        break;
                    }
                }
                List<Long> banquetIdList = activityBanquetIdMap.get(item.getActivityId());
                if(banquetIdList == null){
                    banquetIdList = new ArrayList<>();
                    activityBanquetIdMap.put(item.getActivityId(),banquetIdList);
                }
                banquetIdList.add(item.getBanquetId());
                if(duplicate){
                    // 重复
                    continue;
                }
                GoodBanquetFinishReceiveData finishReceiveData = new GoodBanquetFinishReceiveData();
                finishReceiveData.setBanquetId(item.getBanquetId());
                finishReceiveData.setActivityId(item.getActivityId());
                finishReceiveData.setUserId(item.getUserId());
                finishReceiveData.setEndTime(item.getEndTime());
                finishReceiveData.setType(item.getType());
                finishReceiveData.setNum(item.getNum());
                finishReceiveData.setInsertOption();
                synchronized (userFinishList){
                    userFinishList.add(finishReceiveData);
                }
                set.add(finishReceiveData.getUserId());

                // 加日志
                LogMgr.addLogGoodBanquetRecord(new LogGoodBanquetRecord(finishReceiveData.getActivityId(),finishReceiveData.getUserId(),finishReceiveData.getType(),finishReceiveData.getNum(),finishReceiveData.getEndTime(), DateHelper.getCurrentSecond()));
            }
            // 给跨服反馈
            for(Map.Entry<Integer,List<Long>> entry : activityBanquetIdMap.entrySet()){
                CrossBanquetProto.GoodBanquetReceiveSettlementToCrossReqMsg.Builder toCrossReqMsg = CrossBanquetProto.GoodBanquetReceiveSettlementToCrossReqMsg.newBuilder();
                toCrossReqMsg.setActivityId(entry.getKey());
                toCrossReqMsg.addAllId(entry.getValue());
                GamePlayerMgr.sendPacket(0, YanQuMessageUtils.buildMessage(Protocol.C2_GOOD_BANQUET_RECEIVE_SETTLEMENT_SUCCESS, toCrossReqMsg));
            }
            // 如果玩家在活动界面 直接领奖并通知给他
            for (long userId : set){
                GamePlayer player = GamePlayerMgr.getOnlinePlayer(userId);
                if(player != null){
                    if(CrossBanquetMgr.getInSystemUserSet().contains(userId)){
                        List<CrossBanquetProto.GoodBanquetSettlementReward> list = CrossBanquetMgr.receiveGoodBanquetSettlementReward(player);
                        if(list.size() > 0){
                            player.sendPacket(ClientProtocol.U_CROSS_GOOD_BANQUET_SETTLEMENT_REWARD, CrossBanquetProto.SynGoodBanquetSettlementRewardRespMsg.newBuilder().addAllRewardList(list));
                        }
                    }
                }
            }
        }
    }
}
